package android.com.app;

import android.app.Activity;
import android.app.ListActivity;
import android.com.app.DBAdapter.Table;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class editTag extends ListActivity {
	 
	    private static final int ACTIVITY_EDIT=1;
	    private DBAdapter mDbHelper;
	    private Cursor mCursor;
	    private String titArg;
	    
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.edittag_list);
	        Bundle extras=getIntent().getExtras();
	        titArg=extras.getString("Title");
	        mDbHelper = new DBAdapter(this);
	        mDbHelper.open();
	        fillData();
	    }
	    
	    private void fillData() {
	        // Get all of the rows from the database and create the item list
	    	String[] from = new String[]{"tag"};
	        int[] to = new int[]{R.id.text2};
	        mCursor = mDbHelper.fetch(Table.TAGS, titArg);
	        startManagingCursor(mCursor);
	        
	        // Now create a simple cursor adapter and set it to display
	        try {
	        SimpleCursorAdapter notes = 
	        	    new SimpleCursorAdapter(this, R.layout.edit_row, mCursor, from, to);
	       	setListAdapter(notes);
	        }catch(Exception e) {
	        	Log.i("DEbug2",e.toString()+mCursor.getCount());
	        }
	    }
	    
    
	    @Override
	    protected void onListItemClick(ListView l, View v, int position, long id) {
	        super.onListItemClick(l, v, position, id);
	        Cursor c = mCursor;
	        c.moveToPosition(position);
	        Intent i = new Intent(this, rowEdit.class);
	        i.putExtra("title", c.getString(c.getColumnIndex("title")));
	        i.putExtra("tag", c.getString(c.getColumnIndex("tag")));
	        i.putExtra("id", c.getInt(c.getColumnIndex("_id")));
	        startActivityForResult(i, ACTIVITY_EDIT);
	    }

	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	        super.onActivityResult(requestCode, resultCode, intent);
	        Bundle extras;
	        
	        if(intent!=null) {
	        	extras=intent.getExtras();
	        	switch(requestCode) {
		        
	        		case ACTIVITY_EDIT:
	        			String newTag = extras.getString("newtag");
	        			String title=extras.getString("title");
	        			int oldTag=extras.getInt("oldtag");
	        			if (newTag != null) {
	        				mDbHelper.updateNote(title, oldTag, newTag, Table.TAGS);
	        			}
	        			else {
	        				mDbHelper.DeleteEntry(Table.TAGS, title, oldTag);
	        				//Log.i("deb","nll");
	        			}
	        			fillData();
	        			break;
	        		}
	        }
	    }
	    
		private static final int GAL_ID = Menu.FIRST;
		private static final int CAM_ID = Menu.FIRST+1;
	    
	    
	    @Override
	    public boolean onMenuItemSelected(int featureId, MenuItem item) {
	        switch(item.getItemId()) {
	        case GAL_ID:
	        	startActivity(new Intent( this, thumbviewer.class));
	            return true;
	        case CAM_ID:
	        	startActivity(new Intent( this, aPics.class));
	        	return true;
	        }
	        return super.onMenuItemSelected(featureId, item);
	    }
	    
   }
